//
//  UUProductSpecEditVC.m
//  UUCharterSupplier
//
//  Created by ZhangAo on 26/02/2018.
//  Copyright © 2018 UUCharter. All rights reserved.
//

#import "UUProductSpecEditVC.h"
#import "UUHybridManager+CharterSupplier.h"
#import "UUProductManager.h"
#import "UUSupplierManager.h"
#import <UUFoundation/UIBarButtonItem+CustomButton.h>

@interface UUProductSpecEditVC ()

@end

@implementation UUProductSpecEditVC

- (void)viewDidLoad {
    self.autoManagedEditEntrance = NO;
    
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSString *)name {
    return @"规格";
}

- (BOOL)isAdd {
    return self.spec == nil;
}

- (void)formDidLoad {
    [super formDidLoad];
    
    if (self.editable) {
        self.navigationItem.rightBarButtonItem = [UIBarButtonItem barButtonItemWithTitle:@"保存"
                                                                                  target:self
                                                                                selector:@selector(saveSpec)];
    }
}

- (NSURLRequest *)editRequest {
    return GetHybridManager().productSpecEditURLRequest;
}

- (NSURLRequest *)detailRequest {
    return GetHybridManager().productSpecURLRequest;
}

- (NSDictionary *)previewFields {
    if (self.spec) {
        UUProductPackageModel *package = [GetProductManager() fetchProductPackageModelWithPackageId:self.spec.packageId];
        
        
        NSMutableArray<NSDictionary *> *peakSeasonDicts = [NSMutableArray arrayWithCapacity:self.spec.peakSeason.count];
        for (UUProductSpecPeakSeasonModel *specPeakSeasonModel in self.spec.peakSeason) {
            UUSupplierPeakSeasonModel *peakSeasonModel = [GetSupplierManager() fetchPeakSeasonModelWithId:specPeakSeasonModel.id];
            [peakSeasonDicts addObject:@{
                                         @"id" : peakSeasonModel.id,
                                         @"name" : peakSeasonModel.name,
                                         @"price" : specPeakSeasonModel.price,
                                         }];
        }
        
        NSMutableArray<NSDictionary *> *halfPeakSeasonDicts = [NSMutableArray arrayWithCapacity:self.spec.halfDayPeakSeason.count];
        for (UUProductSpecPeakSeasonModel *specPeakSeasonModel in self.spec.halfDayPeakSeason) {
            UUSupplierPeakSeasonModel *peakSeasonModel = [GetSupplierManager() fetchPeakSeasonModelWithId:specPeakSeasonModel.id];
            [halfPeakSeasonDicts addObject:@{
                                             @"id" : peakSeasonModel.id,
                                             @"name" : peakSeasonModel.name,
                                             @"price" : specPeakSeasonModel.price,
                                             }];
        }
        
        return @{
                 @"index" : @(0),
                 @"type" : @(self.spec.packageType),
                 @"spec_id" : package.id ?: @"",
                 @"name" : package.name ?: @"",
                 @"price" : self.spec.price,
                 @"half_day_price" : self.spec.halfDayPrice ?: @(0),
                 @"is_half_day" : @(package.isHalfDay),
                 @"peak_season" : peakSeasonDicts,
                 @"half_day_peak_season" : halfPeakSeasonDicts,
                 };
    } else {
        return nil;
    }
}

- (void)configEditVC:(UUFormBaseVC *)editVC {
    [(UUProductSpecEditVC *)editVC setSpec:self.spec];
}

- (void)addOrUpdate:(NSDictionary *)data {
    BLOCK_SAFE_CALLS(self.didUpdateSpecBlock, [[UUProductSpecModel alloc] initWithData:data]);
}

- (void)delete {
    BLOCK_SAFE_CALLS_In_Main_Queue(self.didUpdateSpecBlock, nil);
}

- (void)saveSpec {
    [self submit];
}

@end
